package com.wish.g04.business.controller;


import com.wish.g04.base.entity.Business;
import com.wish.g04.business.dto.BusinessDTO;
import com.wish.g04.business.dto.UserDTO;
import com.wish.g04.business.service.BusinessService;
import com.wish.g04.common.core.domain.R;
import com.wish.g04.common.core.web.controller.BaseController;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.core.web.page.TableDataInfo;
import com.wish.g04.common.log.annotation.Log;
import com.wish.g04.common.log.enums.BusinessType;
import com.wish.g04.common.redis.service.RedisService;
import com.wish.g04.system.api.RemoteFileService;
import com.wish.g04.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.wish.g04.common.security.annotation.PreAuthorize;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 商户Controller
 * 
 * @author ruoyi
 * @date 2022-05-27
 */
@RestController
@RequestMapping("/business")
public class TbBusinessController extends BaseController
{

    @Autowired
    private BusinessService businessService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RemoteFileService remoteFileService;

    private final String cache="phone:";

    /**
     * 查询商户列表
     */
    @PreAuthorize(hasPermi = "system:business:list")
    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        return businessService.getBusinessList();
    }

    /**
     * 获取商户详细信息
     */
    @PreAuthorize(hasPermi = "system:business:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(businessService.selectTbBusinessById(id));
    }


    /**
     * 审核不通过
     * @param businessDTO
     * @return
     */
    @PreAuthorize(hasPermi = "system:business:edit")
    @Log(title = "商户审核不通过", businessType = BusinessType.UPDATE)
    @PutMapping("/not_update")
    public AjaxResult edit(@RequestBody BusinessDTO businessDTO)
    {
        Business businessInfo=businessService.getById(businessDTO.getId());
        businessInfo.setStatus(2);
        businessService.notUpdateBusiness(businessInfo);
        return AjaxResult.success();
    }


    /**
     * 审核通过
     * @param businessDTO
     * @return
     */
    @PutMapping("/update")
    @Log(title = "商户审核通过", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody BusinessDTO businessDTO)
    {
        return businessService.update(businessDTO);
    }


    @GetMapping(value = "/getCode/{mobile}")
    public AjaxResult getCode(@PathVariable("mobile")String mobile){
        System.out.println(mobile);
        //生成4位数字验证码
        Random random = new Random();
        String code = "";
        for (int i = 0; i <4 ; i++) {
            code+=random.nextInt(10);
        }
        String key = String.format(cache+mobile);
        redisService.setCacheObject(key,code,new Long(1), TimeUnit.MINUTES);
        return AjaxResult.success(code);
    }

    @PutMapping("/register")
    public AjaxResult register(@RequestBody UserDTO userDTO){
        System.out.println(userDTO);
        return businessService.register(userDTO);
    }

    @GetMapping("/getCityList")
    public AjaxResult getCityList(){
        return businessService.getCityList();
    }

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        return AjaxResult.success(remoteFileService.upload(file));
    }

    @PutMapping("/addBusiness")
    public AjaxResult addBusiness(@RequestBody BusinessDTO businessDTO){
        return businessService.save(businessDTO);
    }








}
